//
//  BirthdayViewController.m
//  QT
//
//  Created by 甘世清 on 2025/1/3.
//

#import "BirthdayViewController.h"
#import "MePhotoCollectionViewCell.h"
#import "PhotoModel.h"
#import <BRPickerView/BRPickerView.h>
#import "SQCLLocationManager.h"
@interface BirthdayViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>


@end

@implementation BirthdayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_hideBaseNavBar = YES;
}
- (void)SQ_UIViewForSelfView{
    
   
    UILabel * label1  = [[UILabel alloc]init];
    label1.text = SQNSLocalString(@"生日");
    label1.font = FONT_MEDIUM(14);
    label1.textColor = SQColor999999;
    [self.view addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(16);
        make.top.equalTo(self.view).offset(30);
    }];
    [self.view addSubview:self.birthday];
    [self.birthday mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDETH-100);
        make.height.mas_equalTo(48);
        make.top.equalTo(label1.mas_bottom).offset(10);
        make.centerX.equalTo(self.view);
    }];
    
    UILabel * label12  = [[UILabel alloc]init];
    label12.text = SQNSLocalString(@"所在地");
    label12.font = FONT_MEDIUM(14);
    label12.textColor = SQColor999999;
    [self.view addSubview:label12];
    [label12 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(16);
        make.top.equalTo(self.birthday.mas_bottom).offset(20);
    }];
    [self.view addSubview:self.city];
    [self.city mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDETH-100);
        make.height.mas_equalTo(48);
        make.top.equalTo(label12.mas_bottom).offset(10);
        make.centerX.equalTo(self.view);
    }];
    
    UILabel * label13  = [[UILabel alloc]init];
    label13.text = SQNSLocalString(@"相册");
    label13.font = FONT_MEDIUM(14);
    label13.textColor = SQColor999999;
    [self.view addSubview:label13];
    [label13 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(16);
        make.top.equalTo(self.city.mas_bottom).offset(20);
    }];
    
    PhotoModel * model = [PhotoModel new];
    model.addImage = SQGetImage(@"header");
    model.isAdd = YES;
    [self.jsonArray addObject:model];
    UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.collectionViewLayout = flowLayout;
    [self.collectionView registerClass:[MePhotoCollectionViewCell class] forCellWithReuseIdentifier:@"MePhotoCollectionViewCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(label13);
            make.top.equalTo(label13.mas_bottom).offset(10);
            make.height.mas_equalTo(130);
            make.width.mas_equalTo(SCREEN_WIDETH-32);
    }];
    self.collectionView.alwaysBounceVertical = NO;
    
    [self.view addSubview:self.reloadBtn];
    [self.reloadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.height.mas_equalTo(50);
            make.width.mas_equalTo(SCREEN_WIDETH-60);
        make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-20);
    }];
    
    WS(weakSelf);
    [self.reloadBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        if (weakSelf.btntouchBlock) {
            weakSelf.btntouchBlock();
        }
    }];
    [self.birthday setTouchBlock:^(SQCustomButton * _Nonnull button) {
            [weakSelf SQ_birthdayButtonClick];
    }];
    [self.city setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_cityBtnClick];
    }];
}

-(void)SQ_birthdayButtonClick{
    WS(weakSelf);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString* selectValue = [NSString SQ_GetoutYear:-24];
    NSString* minDate = [NSString SQ_GetoutYear:-100];
    NSString* maxDate = [NSString SQ_GetoutYear:-18];
    BRDatePickerView * pickView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMD];
    pickView.pickerStyle = [self SQ_getStyle];
    pickView.selectValue = selectValue;
    if ([self.birthday.text isNotEmpty]) {
        pickView.selectValue = self.birthday.text;
    }
    pickView.maxDate = [dateFormatter dateFromString:maxDate];
    pickView.minDate = [dateFormatter dateFromString:minDate];
    [pickView setResultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
        weakSelf.birthday.text = selectValue;
    }];
    [pickView show];
}
-(void)SQ_cityBtnClick{
    WS(weakSelf);
    [[SQCLLocationManager sharedManager]SQ_Location:^(NSDictionary * _Nonnull cityDic) {
        weakSelf.city.text = [cityDic objectForKey:@"city"];
    }];;
}
#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.jsonArray.count<10) {
        return self.jsonArray.count;
    }
    return 9;
}

-(UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    MePhotoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MePhotoCollectionViewCell" forIndexPath:indexPath];
    cell.model = [self.jsonArray objectAtIndex:indexPath.item];
  return cell;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    return CGSizeMake(90 , 90);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    PhotoModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    if (model.isAdd) {
        [self SQ_addImage];
    }else{
        [self SQ_deletedButtonClick:indexPath];
    }
}
-(void)SQ_addImage{
    ImagePopView * vc = [[ImagePopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 200+FootHeight)];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    [pop pop];
    vc.page = 10 - self.jsonArray.count;
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [vc setAddImageArrayBlock:^(NSArray<UIImage *> * _Nonnull imageArray) {
        for (UIImage * image in imageArray) {
            PhotoModel * model = [PhotoModel new];
            model.addImage = image;
            [weakSelf.jsonArray insertObject:model atIndex:0];
        }
        [weakSelf.collectionView reloadData];
        [wk_pop dismiss];
    }];
}

-(void)SQ_deletedButtonClick:(NSIndexPath*)indexPath{
    WS(weakSelf);
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];

     UIAlertAction * co1 = [UIAlertAction actionWithTitle:SQNSLocalString(@"删除") style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [weakSelf.jsonArray removeObjectAtIndex:indexPath.item];
            [weakSelf.collectionView reloadData];
    }];
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:co1];
    [alert addAction:co2];
    [self presentViewController:alert animated:YES completion:nil];
}

-(BRPickerStyle*)SQ_getStyle{
    BRPickerStyle * style = [BRPickerStyle new];
    style.alertViewColor = UIColor.whiteColor;
    style.pickerColor = UIColor.whiteColor;
    style.titleBarColor= UIColor.whiteColor;
    style.hiddenTitleLine = YES;
    style.titleTextColor = SQColor333333;
    style.cancelTextColor = SQColor333333;
    style.doneTextColor = SQColor333333;
    style.rowHeight = 50;
    style.pickerTextColor = SQColor333333;
    style.separatorColor = SQColorDA70D6;
    return style;
}

- (SQCustomButton *)reloadBtn{
    if (!_reloadBtn) {
        _reloadBtn = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(0, 0) midmargin:10];
        _reloadBtn.text = SQNSLocalString(@"下一个");
        _reloadBtn.image = SQGetImage(@"xiayige");
        _reloadBtn.font = FONT_MEDIUM(14);
        _reloadBtn.textColor = UIColor.whiteColor;
        _reloadBtn.layer.cornerRadius = 10;
        _reloadBtn.layer.borderColor = SQColor333333.CGColor;
        _reloadBtn.layer.borderWidth = 0.5;
        _reloadBtn.backgroundColor = SQColorDA70D6;
    }
    return _reloadBtn;
}
- (SQCustomButton *)birthday{
    if (!_birthday) {
        _birthday = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(0, 0) midmargin:0];
        _birthday.textColor = SQColor333333;
        _birthday.font = FONT_MEDIUM(14);
        _birthday.layer.cornerRadius = 24;
        _birthday.layer.borderColor = SQHexColor(@"#000000", 0.2).CGColor;
        _birthday.layer.borderWidth = 0.5;
    }
    return _birthday;
}

- (SQCustomButton *)city{
    if (!_city) {
        _city = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(0, 0) midmargin:0];
        _city.textColor = SQColor333333;
        _city.font = FONT_MEDIUM(14);
        _city.layer.cornerRadius = 24;
        _city.layer.borderColor = SQHexColor(@"#000000", 0.2).CGColor;
        _city.layer.borderWidth = 0.5;
    }
    return _city;
}
@end
